<?php


namespace App\Http\Common;

use App\Models\Csyys\BaseOtherSetting;
use App\Models\Csyys\BusinessList;
use App\Models\Group;
use App\Models\Parkings;
use App\Models\Rooms;
use App\Models\UserGroup;
use App\Models\WyArea;
use App\Models\WyOperaors;
use App\Models\WyPropertys;
use App\Models\WyWechatAccount;

/**
 * 组工具，来操作与组相关的复杂 方法，比如通过用户来查找 是哪个运营商id
 * Class GroupTools
 * @package App\Http\Common
 */
class GroupTools
{
    //创建静态私有的变量保存该类对象
    private static $instance;
    private static $instance2;
    //参数
    public $config;
    //防止直接创建对象
    private function __construct($config){
        $this->config = $config;
    }
    //防止克隆对象
    private function __clone(){
    }
    // 使用单列模式 小程序的
    public static function getInstance($config=null){
        //判断$instance是否是Uni的对象
        //没有则创建
        if (!self::$instance instanceof self) {
            self::$instance = new self($config);
        }
        return self::$instance;
    }

    /**
     * 得到某个用户的运营商id
     * @param $userId 用户id
     * @return int|string
     */
    public function getUserOperatorId($userId){
        $wechatAccount = WyWechatAccount::where('user_id',$userId)->first();
        if ($wechatAccount){
            $appid = MyUtils::sg($wechatAccount,'appid');
            $baseOtherSetting = BaseOtherSetting::where('WECHAT_APP_APPID',$appid)->first();
            return MyUtils::sg($baseOtherSetting,'wy_operator_id');
        }
        // 拿到用户是哪个组的
        $userGroup = UserGroup::where('user_id',$userId)->first();
        $groupId = MyUtils::sg($userGroup,'group_id');
        // 然后通过组来查找是哪个运营商
        $group = Group::where('id',$groupId)->first();
        $groupTableName = MyUtils::sg($group,'group_table_name');
        $wy_operator_id = 0;
        if ($groupTableName=='wy_operator'){// 运营商
            $result = WyOperaors::where('group_id',$groupId)->first();
            $wy_operator_id = MyUtils::sg($result,'id');
        }
        if ($groupTableName=='csyys_business'){// 商家的
            $businessGroup = BusinessList::where('group_id',$groupId)->first();
            $wy_operator_id = MyUtils::sg($businessGroup,'wy_operator_id');
        }
        if ($groupTableName=='wy_property'){// 物业的
            $propertysGroup = WyPropertys::where('group_id',$groupId)->first();
            $wy_operator_id = MyUtils::sg($propertysGroup,'wy_operator_id');
        }
        if ($groupTableName=='wy_area'){// 小区的
            $areaGroup = WyArea::where('group_id',$groupId)->first();
            $property = WyPropertys::where('id',MyUtils::sg($areaGroup,'wy_property_id'))->first();
            $wy_operator_id = MyUtils::sg($property,'wy_operator_id');
        }
        return $wy_operator_id;
    }

    /**
     * 得到某个用户的物业id
     * @param $userId 用户id
     * @return int|string
     */
    public function getUserPropertyId($userId){
        // 拿到用户是哪个组的
        $userGroup = UserGroup::where('user_id',$userId)->first();
        Globals::$success = $userGroup;
        $groupId = MyUtils::sg($userGroup,'group_id');
        // 然后通过组来查找是哪个运营商
        $group = Group::where('id',$groupId)->first();
        $groupTableName = MyUtils::sg($group,'group_table_name');
        $wy_property_id = 0;
        if ($groupTableName=='wy_property'){// 物业的
            $propertysGroup = WyPropertys::where('group_id',$groupId)->first();
            $wy_property_id = MyUtils::sg($propertysGroup,'id');
        }
        if ($groupTableName=='wy_area'){// 小区的
            $areaGroup = WyArea::where('group_id',$groupId)->first();
            $property = WyPropertys::where('id',MyUtils::sg($areaGroup,'wy_property_id'))->first();
            $wy_property_id = MyUtils::sg($property,'id');
        }
        return $wy_property_id;
    }

    /**
     * 得到小区房价
     */
     public function getRoomArea($wy_room_id){
         $room = Rooms::where('id',$wy_room_id)->first();
         $rname = '';
         $uname = '';
         $bname = '';
         $aname = '';
         if ($room){
             $rname = $room->name;
             if ($room->unit){
                 $uname = $room->unit->name;
                 if ($room->unit->build){
                     $bname = $room->unit->build->name;
                     if ($room->unit->build->area) {
                         $aname = $room->unit->build->area->name;
                     }
                 }
             }
         }
         $address = $aname.'-'.$bname.'-'.$uname.'-'.$rname;
         return $address;
     }
    /**
     * 得到某个房间的 小区
     */
     public function getAreaByRoomId($wy_room_id){
         $room = Rooms::where('id',$wy_room_id)->first();
         $aname = '';
         if ($room){
             if ($room->unit){
                 if ($room->unit->build){
                     if ($room->unit->build->area) {
                         $aname = $room->unit->build->area->name;
                     }
                 }
             }
         }
         $address = $aname;
         return $address;
     }

    /**
     * 通过车位的id得到小区名字
     */
     public function getParkingArea($wy_parking_id){
         $parking = Parkings::where('id',$wy_parking_id)->first();
         $aname = '';
         $pname = '';
         if ($parking){
             if ($parking->area){
                 $aname = $parking->area->name;
             }
             $pname = '车位号（'.$parking->num.'）';
         }
         $address = $aname.'-'.$pname;
         return $address;
     }


    /**
     * 得到房间的树形结构 如下所示
     * [
     * {
     * title: '小区1',
     * value: '0-12',// 小区id=12
     * key: '0-0',
     * children: [
     * {
     * title: '楼宇1',
     * value: '0-12-34',// 楼宇id=34
     * key: '0-0-0',
     * children: [
     * {
     * title: '单元1',
     * value: '0-12-34-24',// 单元id=24
     * key: '0-0-0-0',
     * children: [
     * {
     * title: '房间207',
     * value: '0-12-34-24-32',// 房间id=24
     * key: '0-0-0-0-0',
     * },
     * ],
     * },
     * ],
     * },
     * ],
     * },
     * ]
     * @param $rooms
     * @param $pid 小区id 或者物业id 楼宇id 单元id
     * @param int $level
     * @param string $value
     * @return array
     */
     public function getRoomListTree($rooms,$pid,$level=0,$value='0'){
         $tree=[];
         foreach ($rooms as $k=>$row) {
             if($row->parent_id==$pid){
                 $row->level=$level;
                 $row->value=$value.'-'.$row->uid;
                 $row->key=$row->value;
                 $row->children=self::getRoomListTree($rooms,$row->uid,$level+1,$row->value);
                 if (
                     ($level==1 && count($row->children)==0)
                     || ($level==0 && count($row->children)==0)
                 ){// 子为空不的话其父亲就不显示的，房间为空，单元就不显示 0为楼宇 1为房间
                 }else{
                     $tree[]=$row;
                 }
             }
         }
         return $tree;
     }

    /**
     * 拿到某个物业的tablename
     * @param $wy_property_id 物业的id
     * @return
     */
     public function getGroupTableName($wy_property_id){
         $property = WyPropertys::find($wy_property_id);
         $group = Group::find($property->group_id);
         $operator = Group::find($group->parent_id);
         return $operator->group_table_name;
     }
}
